package com.share.device.controller;

import com.share.common.core.web.controller.BaseController;
import com.share.common.core.web.domain.AjaxResult;
import com.share.common.core.web.page.TableDataInfo;
import com.share.common.security.utils.SecurityUtils;
import com.share.device.domain.PowerBank;
import com.share.device.service.IPowerBankService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Tag(name = "充电宝接口管理")
@RestController
@RequestMapping("/powerBank")
public class PowerBankController extends BaseController {

    @Autowired
    private IPowerBankService powerBankService;

    @Operation(summary = "删除充电宝")
    @DeleteMapping("/delete")
    public AjaxResult delete(@RequestBody List<Long> ids) {
        return toAjax(powerBankService.removeBatchByIds(ids));
    }

    @Operation(summary = "修改充电宝")
    @PutMapping("/modify")
    public AjaxResult modify(@RequestBody PowerBank powerBank) {
        // 设置由程序获取的参数
        powerBank.setUpdateBy(SecurityUtils.getUsername());
        powerBank.setUpdateTime(new Date());
        // 调用service的方法实现修改
        int rows = powerBankService.updatePowerBank(powerBank);
        return toAjax(rows);
    }

    @Operation(summary = "新增充电宝")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody PowerBank powerBank) {
        // 设置由程序获取的参数
        powerBank.setCreateBy(SecurityUtils.getUsername()); //添加人 当前后台系统登录人名称
        powerBank.setCreateTime(new Date()); // 也可以使用MyBits Plus自动管理
        powerBank.setUpdateTime(new Date()); // 也可以使用MyBits Plus自动管理
        // 调用service的方法实现添加
        int rows = powerBankService.savePowerBank(powerBank);
        return toAjax(rows);
    }

    @Operation(summary = "根据ID查询充电宝详情")
    @GetMapping(value = "/detail/{id}")
    public AjaxResult detail(@PathVariable("id") Long id) {
        return success(powerBankService.getById(id));
    }

    @Operation(summary = "分页查询充电宝集合")
    @PostMapping("/page")
    public TableDataInfo page(@RequestBody PowerBank powerBank) {
        //设置分页参数
        startPage();
        //调用service
        List<PowerBank> list = powerBankService.selectListPowerBank(powerBank);
        return getDataTable(list);
    }

}
